/**
 * 
 */
package com.qdairlines.controller.dict;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qdairlines.common.ControllerUtil;
import com.qdairlines.common.MultiselectUtil;
import com.qdairlines.common.Page;
import com.qdairlines.constant.BaseConsts;
import com.qdairlines.constant.DictionaryConsts;
import com.qdairlines.constant.RequestConsts;
import com.qdairlines.controller.base.BaseController;
import com.qdairlines.entity.dict.Dictionary;
import com.qdairlines.entity.dict.DictionaryView;
import com.qdairlines.service.dict.DictionaryItemService;
import com.qdairlines.service.dict.DictionaryService;

/**
 * @author pangyannan
 * @date 2017年3月6日
 * @company QingDao Airlines
 * @description 字典控制器
 */
@Controller
@RequestMapping("dict")
public class DictionaryController extends BaseController{
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private DictionaryItemService dictionaryItemService;
	
	/**
	 * 根据字典Code，获取字典数据
	 * @param dictionaryCode
	 * @return
	 */
	@RequestMapping(value = "getMultiselect" , produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getMultiselect(HttpServletRequest request,String dictionaryCode){
		String language = ControllerUtil.getLanguage(request);
		Map<String, String> map = new HashMap<String,String>();	
		List<DictionaryView> list = dictionaryService.listDictionaryView(dictionaryCode);
		
		String defaultVal = null;
		for(DictionaryView d :list){
			if(BaseConsts.EN_US.equals(language)){
				String textEn = d.getTextEn();
				if(!StringUtils.isEmpty(textEn)){
					d.setText(textEn);
				}
			}
			
			if(BaseConsts.ONE.equals(d.getIsDefault())){
				defaultVal = d.getId();
			}
		}
		
		if(list !=null && list.size()==1){
			//如果下拉框只有一个选项，则该选择默认选中
			defaultVal = list.get(0).getId();
		}
		map.put("jsonData", MultiselectUtil.toJosn(list,defaultVal, "id", "text",language));
		map.put("defaultVal", defaultVal);
		
		return ControllerUtil.getJson(map);

	}
	
	
	
	/**
	 * 进入字典管理页面
	 * @return
	 */
    @RequestMapping( value = "toListPage" ,produces =RequestConsts.PRODUCES)
    public String toListPage(HttpServletRequest request,Model model) {
    	model.addAttribute(BaseConsts.LANGUAGE, ControllerUtil.getLanguage(request));
    	return "/dict/dictionarylist";
    }	

    
	/**
	 * 获取单个字典数据
	 * @param request
	 * @param dictionaryId
	 * @return
	 */
    @RequestMapping(value = "getDictionary" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String getDictionary(HttpServletRequest request,Integer dictionaryId) {
    	Dictionary  d = dictionaryService.getByPrimaryKey(dictionaryId);
     	return ControllerUtil.getJson(d);
    }
    
    /**
     * 字典清单数据Page
     * @param request
     * @param dictionary
     * @return
     */
    @SuppressWarnings("unchecked")
	@RequestMapping(value = "pageDictionary",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String pageDictionary(HttpServletRequest request,Dictionary dictionary) {
    	Page page = dictionaryService.getPageFromRequest(request);
    	page = dictionaryService.listData(dictionary, page);
    	
    	List<Dictionary> list= page.getRows();
    	dictionaryService.convertDictValueToText(list, "status", "statusName", DictionaryConsts.DICTIONARY_STATUS, ControllerUtil.getLanguage(request));
    
    	return ControllerUtil.getJson(page);
    }
    
      
    /**
     * 保存字典
     * @param request
     * @param dictionary
     * @return
     */
    @RequestMapping(value = "save",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String save(HttpServletRequest request,Dictionary dictionary) { 
   		//检查编码是否重复
		Dictionary dictionaryCk  = new Dictionary();
		dictionaryCk.setDictionaryCode(dictionary.getDictionaryCode());
		List<Dictionary> list = dictionaryService.listData(dictionaryCk);
		if(list != null && list.size() > 0 ){
			for(Dictionary d : list){
				if(d.getId().equals(dictionary.getId())){
					continue;
				}
				if(dictionary.getDictionaryCode().equals(d.getDictionaryCode())){
					return JSONObject.fromObject(BaseConsts.RESPONSE_MAP_ERROR).toString();
				}
			}
			
		}
		
		
    	if(dictionary.getId() == null){
    		//新增
    		dictionary.setCreateBy(ControllerUtil.getUser(request).getWorkcode());
    		dictionary.setCreateTime(new Date());
    		dictionaryService.saveSelective(dictionary);	
    	}else{
    		//修改
    		dictionary.setUpdateBy(ControllerUtil.getUser(request).getWorkcode());
    		dictionary.setUpdateTime(new Date());
    		dictionaryService.updateByPrimaryKeySelective(dictionary);
    	}
    	
    	return  ControllerUtil.getJson(dictionary);
    }
    
    /**
	 * 进入字典编辑页面
	 * @return
	 */
    @RequestMapping( value = "toEditPage" ,produces =RequestConsts.PRODUCES)
    public String toEditPage(HttpServletRequest request,Integer dictionaryId,Model model) {
    	model.addAttribute(BaseConsts.LANGUAGE, ControllerUtil.getLanguage(request));
    	if(dictionaryId !=null){
    		Dictionary  dictionary = dictionaryService.getByPrimaryKey(dictionaryId);
    		model.addAttribute("dictionary", dictionary);
    		
    	}
    	return "/dict/dictionaryedit";
    }	
    
    @RequestMapping( value = "delete" ,produces =RequestConsts.PRODUCES)
    @ResponseBody
    public String delete(@RequestParam Integer dictionaryId) {
    	//用自定义的删除
    	dictionaryService.deleteByPrimaryKey(dictionaryId);
    	return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }	
    
}
